package com.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.chain.ChainMapper;
import org.apache.hadoop.mapreduce.lib.chain.ChainReducer;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.map.InverseMapper;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.mapreduce.lib.reduce.IntSumReducer;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.net.URI;

public class ChainMapReduce extends Configured implements Tool {
    // 第一个Mapper -> InverseMapper(反转K,V)
    // 第二个Mapper -> 将V变成1
    static class ChainMapReduceMapper extends
            Mapper<Text, Text, Text, IntWritable>{
        @Override
        protected void map(Text key, Text value, Mapper<Text, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            // 将V1:value变成1
            context.write(key,new IntWritable(1));
        }
    }

    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = getConf();
        // 指定keyvaluetextInputFormat的切分的分隔符
        conf.set(
                "mapreduce.input.keyvaluelinerecordreader.key.value.separator",
                ",");
        // 指定输入输出路径
        Path input = new Path(
                "hdfs://192.168.10.11:9000/patentdata");
        Path output = new Path(
                "hdfs://192.168.10.11:9000/chainoutput");

        FileSystem fs = FileSystem.get(
                new URI("hdfs://192.168.10.11:9000")
                ,conf);
        if (fs.exists(output)) fs.delete(output,true);
        //构建Job
        Job job = Job.getInstance(conf);
        job.setJobName("chain");
        job.setJarByClass(this.getClass());
        // 链式Mapper
        // 使用反转Mapper
        ChainMapper.addMapper(job,
                InverseMapper.class,
                Text.class,Text.class,
                Text.class,Text.class,conf);
        // 使用自定义Mapper
        ChainMapper.addMapper(job,
                ChainMapReduceMapper.class,
                Text.class,Text.class,
                Text.class,IntWritable.class,conf);
        // 使用IntSumReducer,对各个key的整型值求和
//        job.setReducerClass(IntSumReducer.class);
//        job.setOutputKeyClass(Text.class);
//        job.setOutputValueClass(IntWritable.class);
        // 链式Reducer
        ChainReducer.setReducer(job,IntSumReducer.class,
                Text.class,IntWritable.class,
                Text.class,IntWritable.class,
                conf);
        // 使用反转MAP
        ChainReducer.addMapper(job,InverseMapper.class,
                Text.class,IntWritable.class,
                IntWritable.class,Text.class,
                conf);


        job.setInputFormatClass(KeyValueTextInputFormat.class);
        KeyValueTextInputFormat.addInputPath(job,input);

        TextOutputFormat.setOutputPath(job,output);
        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(ToolRunner.run(new ChainMapReduce(),args));
    }
}
